/*
 * @Author: 杨上漂
 * @Date: 2024-10-13 10:27:42
 * @LastEditors: 杨上漂
 * @LastEditTime: 2024-10-16 18:13:32
 * @FilePath: \gwlc\assets\scripts\FireBall.ts
 * @Description: 
 * 
 * Copyright (c) 2024 by ${git_name_email}, All Rights Reserved. 
 */
import { _decorator, BoxCollider2D, Collider, Collider2D, Component, Contact2DType, Director, director, dragonBones, ITriggerEvent, log, Node, v2, v3 } from 'cc';
import { EnemyMgr } from '../mainScene/EnemyMgr';
import { playerSkillTypeEnum } from '../Enum';
import { Enemy } from '../mainScene/Enemy';
import { PoolMgr } from '../PoolMgr';

const { ccclass, property } = _decorator;

@ccclass('LightFlash')
export class LightFlash extends Component {

    attackTargetPos=null;
    attackEnemy=null;

    tempPos=v3();

    init() {
        this.poolReset();
        this.node.getChildByName("sp").getComponent(dragonBones.ArmatureDisplay).playAnimation("Flash",1)
        //获取目标敌人
        let attackArr = EnemyMgr.ins.getAnyExistFourEnemyNode()
        if (attackArr) {
            let enemy = attackArr[Math.round(Math.random() * (attackArr.length - 1))]
            this.attackTargetPos = enemy.position;
            this.attackEnemy = enemy;
        }
        else {
            //场上没敌人停止射击
        }

        this.node.setPosition(this.attackTargetPos.x, this.attackTargetPos.y + 60);
        this.attackEnemy.getComponent(Enemy).hurt(playerSkillTypeEnum.light);

        // let collider=this.node.getComponent(BoxCollider2D);
        // collider.on(Contact2DType.BEGIN_CONTACT, this.onTriggerFuc, this);

        setTimeout(() => {
            this.recycle();
        }, 500)
    }

    poolReset() {//重新设置，针对预制体池
        this.attackTargetPos = null;
        this.tempPos = v3();
        this.attackEnemy = null;
    }

    // onTriggerFuc(self: Collider, other: Collider) {
    //         if (other.node.getComponent(BoxCollider2D).tag==1) {
    //             director.once(Director.EVENT_AFTER_PHYSICS,()=>{
    //                 other.node.getComponent(Enemy).hurt(playerSkillTypeEnum.light);
    //             },this)
    //         }
    // }

    recycle(){
        // this.node.destroy();
        PoolMgr.ins.poolPut(this.node.name,this.node);
    }
 
 
    
 
}


